package com.block.datapush.common.filter.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class FilterOrFilterHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        String filterValue = handlerParam.getOrFilter();
        if (StrUtil.isEmpty(filterValue)){
            return FilterHandlerResult.success();
        }

        List<String> valueList = JSON.parseArray(filterValue, String.class);
        if (CollUtil.isEmpty(valueList)){
            return FilterHandlerResult.success();
        }

        // 用户的条件
        List<String> userValueList = new ArrayList<>();
        userValueList.add(memberExtInfo.getHouse());
        userValueList.add(memberExtInfo.getCar());
        userValueList.add(memberExtInfo.getGjj());
        userValueList.add( memberExtInfo.getYuqi());
        userValueList.add(memberExtInfo.getInsurance());
        userValueList.add(memberExtInfo.getSocialInsurance());
        userValueList.add(memberExtInfo.getEnterpriseOwner());

        setHaveBaiTiaoValue(memberExtInfo.getBaitiao(), userValueList);
        setHaveHuaBeiValue(memberExtInfo.getHuabei(), userValueList);
        setHaveZhimaValue(memberExtInfo.getZhimaScore(), userValueList);
        setHaveXueLiValue(memberExtInfo.getXueli(), userValueList);

        // 获取资质的交集
        Collection<String> resultList = CollUtil.intersection(userValueList, valueList);
        if (CollUtil.isNotEmpty(resultList)){
            // 任选一不为空，满足条件，
            return FilterHandlerResult.success();
        }

        if(valueList.contains("gjj002") && !memberExtInfo.getGjj().equals("gjj001")){
            return FilterHandlerResult.success();
        }

        if(valueList.contains("si002") && !memberExtInfo.getSocialInsurance().equals("si001")){
            return FilterHandlerResult.success();
        }

        if(valueList.contains("in002") && !memberExtInfo.getInsurance().equals("in001")){
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【resultList】{}【valueList】{}【userValueList】{}",
                JSON.toJSONString(resultList), JSON.toJSONString(valueList), JSON.toJSONString(userValueList));
        log.info("【机构条件判断】false【任选一】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.ORFILTER_ERROR, message);
    }


    /**
     * 如果
     * @param zhima
     * @param userValueList
     * @return
     */
    private void setHaveZhimaValue(String zhima, List<String> userValueList){
        if (StrUtil.equals(zhima, BaseTypeInfoEnum.ZhimaScore.zm001.getValue())){
            userValueList.add(BaseTypeInfoEnum.ZhimaScore.zm001.getValue());
        }else{
            userValueList.add(BaseTypeInfoEnum.ZhimaScore.zm000.getValue());
        }
    }

    private void setHaveBaiTiaoValue(String baiTiao, List<String> userValueList){
        if (StrUtil.equals(baiTiao, BaseTypeInfoEnum.Baitiao.bt001.getValue())){
            userValueList.add(BaseTypeInfoEnum.Baitiao.bt001.getValue());
        }else{
            userValueList.add(BaseTypeInfoEnum.Baitiao.bt000.getValue());
        }
    }

    private void setHaveHuaBeiValue(String huaBei, List<String> userValueList){
        if (StrUtil.equals(huaBei, BaseTypeInfoEnum.Huabei.hb001.getValue())){
            userValueList.add(BaseTypeInfoEnum.Huabei.hb001.getValue());
        }else{
            userValueList.add(BaseTypeInfoEnum.Huabei.hb000.getValue());
        }
    }

    private void setHaveXueLiValue(String xueLi, List<String> userValueList){
        if (StrUtil.equals(xueLi, BaseTypeInfoEnum.XueLi.xl001.getValue())){
            userValueList.add(BaseTypeInfoEnum.XueLi.xl001.getValue());
        }else{
            userValueList.add(BaseTypeInfoEnum.XueLi.xl000.getValue());
        }
    }


}
